local EnemyGroup = require("Controller/EnemyGroup");

EnemyFactory = {};
local this = EnemyFactory;
local index = 1;
local total = 4;

local spwnPoint = Vector3(0, 7, 0);

function EnemyFactory.New()
	this.CreateGroup();	
end

function EnemyFactory.CreateGroup()
	if(index > total) then
		index = 1;
	end
	local name = "EnemyGroup" .. index;
	index = index + 1;
	
	resMgr:LoadPrefab("prefabs", {name}, this.OnLoadOK);
end

function EnemyFactory.OnLoadOK(objs)
	local go = newObject(objs[0]);
	go.transform.position = spwnPoint;
	
	this.curGroup = EnemyGroup.new(go);
	
	coroutine.start(this.CheckEnemyCount);
end

function EnemyFactory.CheckEnemyCount()
	while true do
		coroutine.wait(0.5);
		
		if(this.curGroup:ChildCount() <= 0) then
			this.curGroup:Destroy();
			this.CreateGroup();
			break;
		end
	end
end

